package com.chenjt.springai.eight_vectorStore.config;

import com.chenjt.springai.eight_vectorStore.constants.SystemConstants;
import com.chenjt.springai.eight_vectorStore.tools.CourseTools;
import org.springframework.ai.chat.client.ChatClient;
import org.springframework.ai.chat.client.advisor.MessageChatMemoryAdvisor;
import org.springframework.ai.chat.client.advisor.SimpleLoggerAdvisor;
import org.springframework.ai.chat.memory.ChatMemory;
import org.springframework.ai.chat.memory.InMemoryChatMemoryRepository;
import org.springframework.ai.chat.memory.MessageWindowChatMemory;
import org.springframework.ai.ollama.OllamaChatModel;
import org.springframework.ai.openai.OpenAiChatModel;
import org.springframework.ai.openai.OpenAiEmbeddingModel;
import org.springframework.ai.vectorstore.SimpleVectorStore;
import org.springframework.ai.vectorstore.VectorStore;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * 注释：
 *
 * @author chenjt
 * @date 2025/5/27 9:23
 */
@Configuration
public class CommonConfiguration {

    @Bean
    public ChatMemory chatMemory(){
//        return new InMemoryChatMemoryRepository();
//        return new MessageWindowChatMemory();
        return MessageWindowChatMemory.builder()
                .chatMemoryRepository(new InMemoryChatMemoryRepository())
                .maxMessages(10)
                .build();
    }


    /**
     * 如果用的是Redis或ElasticSearch或Chroma等其他向量数据库的话，这里是不用配置的
     * 使用SimpleVectorStore才需要配置
     * @param embeddingModel
     * @return
     */
    @Bean
    public VectorStore vectorStore(OpenAiEmbeddingModel embeddingModel){
        return SimpleVectorStore.builder(embeddingModel).build();
    }


    @Bean
    public ChatClient chatClient(OllamaChatModel chatModel,ChatMemory chatMemory){
        return ChatClient
                .builder(chatModel)
                .defaultSystem("你是一个热心、可爱的智能助手，你的名字叫陈橙晨，请以陈橙晨的身份和语气回答问题")
                .defaultAdvisors(
                        new SimpleLoggerAdvisor(),
                        MessageChatMemoryAdvisor.builder(chatMemory).build()
                )
                .build();
    }




    @Bean
    public ChatClient gameChatClient(OpenAiChatModel chatModel, ChatMemory chatMemory){
        return ChatClient
                .builder(chatModel)
                .defaultSystem(SystemConstants.GAME_SYSTEM_PROMPT)
                .defaultAdvisors(
                        new SimpleLoggerAdvisor(),
                        MessageChatMemoryAdvisor.builder(chatMemory).build()
                )
                .build();
    }





    @Bean
    public ChatClient serviceChatClient(OpenAiChatModel chatModel,
                                        ChatMemory chatMemory,
                                        CourseTools courseTools){
        return ChatClient
                .builder(chatModel)
                .defaultSystem(SystemConstants.SERVICE_SYSTEM_PROMPT)
                .defaultAdvisors(
                        new SimpleLoggerAdvisor(),
                        MessageChatMemoryAdvisor.builder(chatMemory).build()
                )
                .defaultTools(courseTools)
                .build();
    }




}
